#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/13 18:46
# @Author  : leon yan
# @Site    : 
# @File    : qChartDrawing.py
# @Software: PyCharm
import random
import time

from PyQt5.QtChart import QPieSeries, QBarSet, QLineSeries, QBarSeries, QHorizontalBarSeries, QAbstractBarSeries, \
    QBarCategoryAxis, QValueAxis
from PyQt5.QtCore import QObject, Qt
from PyQt5.QtGui import QPen
from datetime import date, timedelta
import Globalist as gl


def lasted5day():
    dt = []
    for i in range(5):
        aa = str(date.today() - timedelta(i))
        dt.append(aa[5:])
    return dt


class QChartDrawing(QObject):
    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent)
        pass

    def draw_pieChart(self, chartObj):  # 绘制饼图
        # chart = self.chartViewPie.chart()  # 获取chart对象
        chart = chartObj
        chart.legend().setAlignment(Qt.AlignLeft)  # AlignRight,AlignBottom
        chart.legend().setVisible(True)
        chart.removeAllSeries()  # 删除所有序列

        seriesPie = QPieSeries()  # 饼图序列
        seriesPie.setHoleSize(0.35)  # 饼图中间空心的大小
        seriesPie.setPieSize(0.75)
        seriesPie.setLabelsVisible(True)  # 只影响当前的slices，必须添加完slice之后再设置

        timeMonitor = gl.global_var["chartMonitor"]["timeMonitor"]
        for i in range(len(timeMonitor)):
            sliceLabel = timeMonitor[i][0] + "(" + str(timeMonitor[i][1]) + "%)"
            sliceValue = timeMonitor[i][1]
            seriesPie.append(sliceLabel, sliceValue)  # 添加一个饼图分块数据,(标签，数值)

        seriesPie.setLabelsVisible(True)  # 只影响当前的slices，必须添加完slice之后再设置
        seriesPie.hovered.connect(self.do_pieHovered)  # 鼠标落在某个分块上时，此分块弹出
        chart.addSeries(seriesPie)
        # chart.setTitle("Piechart---ssss")

    def do_pieHovered(self, pieSlice, state):  # 鼠标在饼图上移入移出
        pieSlice.setExploded(state)  # 弹出或缩回，具有动态效果
        if state:  # 显示带百分数的标签
            self.__oldLabel = pieSlice.label()  # 保存原来的Label
            pieSlice.setLabel(self.__oldLabel + ": %.1f%%"
                              % (pieSlice.percentage() * 100))
        else:  # 显示原来的标签
            pieSlice.setLabel(self.__oldLabel)

    def draw_barChart(self, chartObj, isVertical=True):  # 绘制柱状图，或水平柱状图
        chart = chartObj
        chart.removeAllSeries()  # 删除所有序列
        chart.removeAxis(chart.axisX())  # 删除坐标轴
        chart.removeAxis(chart.axisY())  # 删除坐标轴
        if isVertical:
            # chart.setTitle("Barchart 演示")
            chart.legend().setAlignment(Qt.AlignBottom)
        else:
            # chart.setTitle("Horizontal Barchart 演示")
            chart.legend().setAlignment(Qt.AlignRight)

        setMath = QBarSet("天平方数")  # QBarSet

        seriesLine = QLineSeries()  # QLineSeries序列用于显示平均分
        seriesLine.setName("周平方数")
        pen = QPen(Qt.red)
        pen.setWidth(2)
        seriesLine.setPen(pen)

        seriesLine.setPointLabelsVisible(True)  # 数据点标签可见
        if isVertical:
            seriesLine.setPointLabelsFormat("@yPoint")  # 显示y数值标签
        else:
            seriesLine.setPointLabelsFormat("@xPoint")  # 显示x数值标签

        font = seriesLine.pointLabelsFont()
        font.setPointSize(10)
        font.setBold(True)
        seriesLine.setPointLabelsFont(font)

        nameList = lasted5day()     # 用于QBarCategoryAxis类坐标轴
        setMath.append(float(random.randint(950, 1000)) / 10.0)
        setMath.append(float(random.randint(750, 900)) / 10.0)
        setMath.append(float(random.randint(850, 900)) / 10.0)
        setMath.append(float(random.randint(950, 1100)) / 10.0)
        setMath.append(float(random.randint(450, 900)) / 10.0)

        aa = [56, 44, 24, 35, 76]
        for i in range(5):
            if isVertical:
                seriesLine.append(i, aa[i])  # 平均分,用于柱状图
            else:
                seriesLine.append(aa[i], i)  # 平均分，用于水平柱状图

        # 创建一个序列 QBarSeries, 并添加三个数据集
        if isVertical:
            seriesBar = QBarSeries()  # 柱状图
        else:
            seriesBar = QHorizontalBarSeries()  # 水平柱状图

        seriesBar.append(setMath)  # 添加数据集
        seriesBar.setLabelsVisible(True)  # 数据点标签可见
        seriesBar.setLabelsFormat("@value")  # 显示数值标签
        seriesBar.setLabelsPosition(QAbstractBarSeries.LabelsCenter)  # 数据标签显示位置
        # seriesBar.hovered.connect(self.do_barSeries_Hovered)  # hovered信号
        # seriesBar.clicked.connect(self.do_barSeries_Clicked)  # clicked信号

        chart.addSeries(seriesBar)  # 添加柱状图序列
        chart.addSeries(seriesLine)  # 添加折线图序列

        ##学生姓名坐标轴
        axisStud = QBarCategoryAxis()
        axisStud.append(nameList)  # 添加横坐标文字列表
        axisStud.setRange(nameList[0], nameList[5 - 1])  # 这只坐标轴范围

        # 数值型坐标轴
        axisValue = QValueAxis()
        axisValue.setRange(0, 110)
        axisValue.setTitleText("平方数")
        axisValue.setTickCount(6)
        axisValue.applyNiceNumbers()
        #    axisValue.setLabelFormat("%.0f") #标签格式
        #    axisY.setGridLineVisible(false)
        #    axisY.setMinorTickCount(4)
        if isVertical:
            chart.setAxisX(axisStud, seriesBar)  # seriesBar
            chart.setAxisY(axisValue, seriesBar)
            chart.setAxisX(axisStud, seriesLine)  # seriesLine
            chart.setAxisY(axisValue, seriesLine)
        else:
            chart.setAxisX(axisValue, seriesBar)  # seriesBar
            chart.setAxisY(axisStud, seriesBar)
            chart.setAxisY(axisStud, seriesLine)  # seriesLine
            chart.setAxisX(axisValue, seriesLine)

        # for marker in chart.legend().markers():  # QLegendMarker类型列表
        #     marker.clicked.connect(self.do_LegendMarkerClicked)

    def draw_barChart2(self, chartObj, isVertical=True):  # 绘制柱状图，或水平柱状图
        chart = chartObj
        chart.removeAllSeries()  # 删除所有序列
        chart.removeAxis(chart.axisX())  # 删除坐标轴
        chart.removeAxis(chart.axisY())  # 删除坐标轴
        if isVertical:
            # chart.setTitle("Barchart 演示")
            chart.legend().setAlignment(Qt.AlignBottom)
        else:
            # chart.setTitle("Horizontal Barchart 演示")
            chart.legend().setAlignment(Qt.AlignRight)

        setMath = QBarSet("kwh")  # QBarSet

        seriesLine = QLineSeries()  # QLineSeries序列用于显示平均分
        seriesLine.setName("***")
        pen = QPen(Qt.red)
        pen.setWidth(2)
        seriesLine.setPen(pen)

        seriesLine.setPointLabelsVisible(True)  # 数据点标签可见
        if isVertical:
            seriesLine.setPointLabelsFormat("@yPoint")  # 显示y数值标签
        else:
            seriesLine.setPointLabelsFormat("@xPoint")  # 显示x数值标签

        font = seriesLine.pointLabelsFont()
        font.setPointSize(10)
        font.setBold(True)
        seriesLine.setPointLabelsFont(font)

        # stud_Count = self.dataModel.rowCount()
        nameList = lasted5day() # 用于QBarCategoryAxis类坐标轴
        dayPowerList = gl.global_var["chartMonitor"]["dayPower"]
        setMath.append(dayPowerList[0])
        setMath.append(dayPowerList[1])
        setMath.append(dayPowerList[2])
        setMath.append(dayPowerList[3])
        setMath.append(dayPowerList[4])

        aa = gl.global_var["chartMonitor"]["dayPower"]
        for i in range(5):
            if isVertical:
                seriesLine.append(i, aa[i])  # 平均分,用于柱状图
            else:
                seriesLine.append(aa[i], i)  # 平均分，用于水平柱状图

        # 创建一个序列 QBarSeries, 并添加三个数据集
        if isVertical:
            seriesBar = QBarSeries()  # 柱状图
        else:
            seriesBar = QHorizontalBarSeries()  # 水平柱状图

        seriesBar.append(setMath)  # 添加数据集
        # seriesBar.append(setChinese)
        # seriesBar.append(setEnglish)
        seriesBar.setLabelsVisible(True)  # 数据点标签可见
        seriesBar.setLabelsFormat("@value")  # 显示数值标签
        seriesBar.setLabelsPosition(QAbstractBarSeries.LabelsCenter)  # 数据标签显示位置
        # seriesBar.hovered.connect(self.do_barSeries_Hovered)  # hovered信号
        # seriesBar.clicked.connect(self.do_barSeries_Clicked)  # clicked信号

        chart.addSeries(seriesBar)  # 添加柱状图序列
        # chart.addSeries(seriesLine)  # 添加折线图序列

        ##学生姓名坐标轴
        axisStud = QBarCategoryAxis()
        axisStud.append(nameList)  # 添加横坐标文字列表
        axisStud.setRange(nameList[0], nameList[5 - 1])  # 这只坐标轴范围

        # 数值型坐标轴
        axisValue = QValueAxis()
        axisValue.setRange(0, max(dayPowerList))
        axisValue.setTitleText("日功耗")
        axisValue.setTickCount(6)
        axisValue.applyNiceNumbers()
        #    axisValue.setLabelFormat("%.0f") #标签格式
        #    axisY.setGridLineVisible(false)
        #    axisY.setMinorTickCount(4)
        if isVertical:
            chart.setAxisX(axisStud, seriesBar)  # seriesBar
            chart.setAxisY(axisValue, seriesBar)
            chart.setAxisX(axisStud, seriesLine)  # seriesLine
            chart.setAxisY(axisValue, seriesLine)
        else:
            chart.setAxisX(axisValue, seriesBar)  # seriesBar
            chart.setAxisY(axisStud, seriesBar)
            chart.setAxisY(axisStud, seriesLine)  # seriesLine
            chart.setAxisX(axisValue, seriesLine)

        # for marker in chart.legend().markers():  # QLegendMarker类型列表
        #     marker.clicked.connect(self.do_LegendMarkerClicked)
